const firstDayInWeek = 6;//以周几为一周第一天，0:周日，1:周一，2:周二，3:周三，4:周四，5:周五，6:周六



export function CalculateWeeks(dateValue: Date): number{
  const date = new Date(dateValue.getFullYear(),dateValue.getMonth(),dateValue.getDate());
  const weeks =  GetPreffixValue(date) + GetFullWeeks(date) + GetSuffixValue(date);
  return weeks;
}


function GetFirstDate(date: Date): Date{
  return new Date(date.getFullYear(),0,1);
}

function GetPreffixDays(date: Date): number{
  const firstDay = GetFirstDate(date).getDay();
  if (firstDayInWeek < firstDay ){
    return firstDayInWeek + 7 - firstDay;
  }
  return firstDayInWeek - firstDay;
}

function GetPreffixValue(date: Date): number{
  if (GetPreffixDays(date) == 0) return 0;
  return 1;
}

function GetDiffDays(date: Date): number{
  const endDate = new Date(date.getFullYear(),date.getMonth(),date.getDate())
    return (endDate - GetFirstDate(date)) / (1000 * 3600 * 24);
}

function GetFullWeeks(date: Date): number{
  const diffDays = GetDiffDays(date);
  const preffixDays = GetPreffixDays(date);
  //如果天数差小于等于前缀天数，则默认0周
  if (diffDays <= preffixDays){
    return 0;
  }
  return Math.floor((diffDays + 1 - preffixDays)/7);
}

function GetSuffixDays(date: Date): number{
  const diffDays = GetDiffDays(date);
  const preffixDays = GetPreffixDays(date);
  //如果天数差小于前缀天数，则默认剩余0天
  if (diffDays + 1 < preffixDays){
    return 0;
  }
  return (diffDays + 1 - preffixDays)%7;
}

function GetSuffixValue(date: Date): number{
    return GetSuffixDays(date) == 0 ? 0 : 1;
}
